<?php
/*=============================================================================
|| ##################################################################
||	phpFoX Konsort
|| ##################################################################
||
||	Copyright		: (C) 2005-2007 PHPFOX Limited a Reality Fox Creation
||	Contact			: info@phpfox.com
||
||	- phpFoX and all of its source code and files are protected by Copyright Laws.
||
||	- The license for phpFoX permits you to install this software on a single domain (.com, .org, .net, etc.).
||
||	- You may also not remove this copyright screen which shows the copyright information and credits for phpFoX (RealityFOX Creations).
||
||	- phpFoX Konsort is NOT a FREE software - http://www.phpfox.com/license/
||
|| ##################################################################
=============================================================================*/


class PhpFox_Mod_Video_ComponentUploadFile extends BaseComponent
{
    function process()
    {
    	App::checkUserAccess('video', 'full');

    	if ( !App::getParam('vid_use_ffmpeg') )
    	{
    		App::gotoUrl('public.account');
    	}

    	App::loadClass('Validator');
        App::loadClass('file/File');
        $oFile = &new CFile();

        $aFields = array
        (
        	'val[vid_title]'   => array('title' => App::format('Mod_Video.add_form_title'), 'def' => 'required'),
        );
        $oValidator = &new Validator($aFields, array());

		$oReq = &App::getRequest();

		$oSrvVideo = &$this->_oModule->getService('Video');

        list($iMemMaxUpload, $iUsedSpace, $iAllowedSpace, $iMaxUploadSize) = App::checkUserSize();

		if ($oReq->get('post_item'))
		{
			$oFile->aLimits['FILE_MAX_SIZE'] = (App::getMemConfig("vid_file_size") == 'iamnatio' ? 0 : App::getMemConfig("vid_file_size"));

		    if (!$oValidator->isValid($oReq->getAll()))
            {
                $this->addErrors($oValidator->getErrors());
            }

			$aPost = $oReq->getArray('val');

			if ( !$oSrvVideo->isAllowedExt($_FILES['video']['name']) )
			{
				$this->addErrors(App::format('Mod_Video.upload_file_invalid', App::getParam('vid_allowed_ext')));
			}

			if (!count($this->getErrors()))
            {
            	$aPost['vid_type'] =  $oFile->getFileType($_FILES['video']['name']);

            	if ( App::getParam('vid_approve') )
            	{
					$iUploadedId = $oSrvVideo->addPendingVideo($aPost);
            	}
            	else
            	{
            		$iUploadedId = $oSrvVideo->addVideo($aPost);
            	}

				$sDestPath = App::getParam('video.path') . (App::getParam('vid_approve') ? "pending_" : '');

                if ($oFile->upload('video', $sDestPath, $iUploadedId, $aPost['vid_type']))
                {
                    $oFile->loadInfo($sDestPath.$iUploadedId.'.'. $aPost['vid_type'].'');
                    $oFile->_checkLimits();
                }

				$this->addErrors($oFile->getErrors());

				if ( App::getParam('vid_use_ffmpeg') )
				{
					$oSrvVideo->convertVideo($iUploadedId, $aPost['vid_type']);
				}

				if ( count($this->getErrors()) )
				{
					(App::getParam('vid_approve') ? $oSrvVideo->deletePendingVideo($iUploadedId) : $oSrvVideo->deleteVideo($iUploadedId));
				}

				$sDestPath = App::getParam('video.image.path') . (App::getParam('vid_approve') ? "pending_" : '');

				if ( !count($this->getErrors()) && !empty($_FILES['image']['name']) )
				{
					App::loadClass('file/Image');
        			$oImage = &new file_Image();

                	if ($oImage->upload('image', $sDestPath, $iUploadedId, 'jpg'))
                	{
                	    $oImage->loadInfo($sDestPath.$iUploadedId.'.jpg');
                	    $oImage->_checkLimits();
                	}

 					$this->addErrors($oImage->getErrors());

	                if (!count($this->getErrors()))
	                {
	                	$oImage->makeThumbnail($sDestPath.$iUploadedId.'.jpg', App::getSetting('upload_set_size_thumb'), App::getSetting('upload_set_size_thumb'));
	                }
				}

				if ( isset($iUploadedId) && !count($this->getErrors()) )
				{
					if ( !file_exists($sDestPath.$iUploadedId.'.jpg') && !App::getParam('vid_approve') )
					{
						$bNoImageUploaded = true;
					}

					App::updateUserSpace();

					if ( !isset($bNoImageUploaded) )
					{
						App::gotoUrl('public.account.submit.add-video', array('mode' => $oReq->get('mode'), 'uploaded' => $iUploadedId));
					}
				}
            }
		}

		if ( $oReq->get('imageid') )
		{
			$bNoImageUploaded = true;
		}

        $this->oTpl->assignSrc('sValMess', $oValidator->makeJsMess());
        $this->oTpl->assignSrc('sValJs', $oValidator->makeJS());
		$this->oTpl->assign(array
		(
			'bApprovalSetting' => App::getParam('vid_approve'),
        	'aErrors' => $this->getErrors(),
			'aItems' => $oSrvVideo->getCategories(),
			'aStreamOptions' => App::getParam('Mod_Video.stream_sources'),
			'iUploadedId' => (isset($iUploadedId) ? $iUploadedId : $oReq->get('uploaded')),
			'iUsedSpace'     => $iUsedSpace,
            'iAllowedSpace'  => $iAllowedSpace,
            'iMaxUploadSize' => $iMaxUploadSize,
            'sMaxFileSize'   => floor($iMaxUploadSize / 1024),
            'bFfmpeg' => App::getParam('vid_use_ffmpeg'),
            'bNoImageUploaded' => (isset($bNoImageUploaded) ? true : false),
            'aVal' => (isset($aPost) ? $aPost : ''),
		));

		return $this->oTpl->fetch('UploadFile.html');
    }
}
?>
